package com.example.e.Adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.e.Bean.Shop
import com.example.e.R

class ShopAdapter(val shopList: List<Shop>): RecyclerView.Adapter<ShopAdapter.ShopViewHolder>() {

    var mOnRecyclerViewItemClick: OnRecyclerViewItemClick<String>? = null
    lateinit var onItemClick: (v: View, pos: Int) -> Unit

    class ShopViewHolder(view: View) : RecyclerView.ViewHolder(view){
        //获取对应的控件
        val tvName : TextView = view.findViewById(R.id.tvName)
        val tvImageURL : ImageView = view.findViewById(R.id.tvImageURL)
        val tvScore : TextView = view.findViewById(R.id.tvScore)
        val tvSale : TextView = view.findViewById(R.id.tvSale)
        val tvSprice : TextView = view.findViewById(R.id.tvSprice)
        val tvDprice : TextView = view.findViewById(R.id.tvDprice)
        val tvDistance : TextView = view.findViewById(R.id.tvDistance)
        val tvTime : TextView = view.findViewById(R.id.tvTime)
    }

    //创建视图
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ShopAdapter.ShopViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.home_shop_layout,parent,false)
        return ShopAdapter.ShopViewHolder(view)
    }

    //
    override fun onBindViewHolder(holder: ShopViewHolder, position: Int) {
        val info = shopList[position]
        holder.tvName.text = info.name
        //holder.tvImageURL.setImageResource(info.imageURL)
        //holder.tvImageURL.text = info.imageURL
        holder.tvScore.text = info.score
        holder.tvSale.text = info.sale
        holder.tvSprice.text = info.sprice
        holder.tvDprice.text = info.dprice
        holder.tvDistance.text = info.distance
        holder.tvTime.text = info.time
        val url : String = info.imageURL
        Glide.with(holder.tvImageURL)
            .load(url)
            .into(holder.tvImageURL)

        holder.itemView.setOnClickListener {
            onItemClick.invoke(it, position)
        }
    }
    public interface OnRecyclerViewItemClick<T> {

        fun OnItemClick(view: View?, t: T?, position: Int)

    }

    //列表的行数
    override fun getItemCount() =  shopList.size
}